public class SolutionLCP12 {
    public int minTime(int[] time, int m) {
        int l=0,r=Integer.MAX_VALUE,ans=-1;
        while (l<=r){
            int mid=(r-l)/2+l;
            int start=0;
            for (int i=0;i<m && start<time.length;i++){
                int num=0,sum=0;
                while (start<time.length){
                    num=Math.max(num,time[start]);
                    sum+=time[start];
                    if (sum-num<=mid){
                        start++;
                    }else {
                        break;
                    }
                }
            }
            if (start==time.length){
                ans=mid;
                r=mid-1;
            }else {
                l=mid+1;
            }
        }
        return ans;
    }

    public static void main(String[] args) {
        System.out.println(new SolutionLCP12().minTime(new int[]{1,2,3,3},2));
    }
}
